package com.arpa.oms.controller;


import cn.hutool.core.map.MapUtil;
import cn.hutool.http.server.HttpServerRequest;
import com.arpa.core.utils.collection.CollectionUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.dto.OmsAutoCheckDTO;
import com.arpa.oms.domain.entity.OmsAutoCheck;
import com.arpa.oms.service.IOmsAutoCheckService;
import com.arpa.wms.domain.consts.PropertyConst;
import com.arpa.wms.domain.enums.StatusEnum;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 自动审核规则 前端控制器
 * </p>
 *
 * @author spb
 * @since 2021-07-24
 */
@Api(tags = "自动审单管理")
@Log4j2(topic = "business")
@RestController
@RequestMapping("/oms/autoCheck")
public class OmsAutoCheckController {

    @Autowired
    private IOmsAutoCheckService iOmsAutoCheckService;

    @ApiOperation(value = "获取 自动审单 列表", notes = "获取 自动审单 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsAutoCheck.class))
    @NeedLogin
    @GetMapping
    public Result list(OmsAutoCheckDTO autoCheckDTO, HttpServletRequest request) {
        try {
            Page returnPage = new Page(autoCheckDTO.getPageNum(), autoCheckDTO.getPageSize());
            Integer count = iOmsAutoCheckService.listCount(autoCheckDTO);
            List<OmsAutoCheck> list = iOmsAutoCheckService.listPage(autoCheckDTO);
            returnPage.setRecords(list);
            returnPage.setTotal(count);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "新增 自动审单 列表", notes = "新增 自动审单 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping
    public Result create(@RequestBody OmsAutoCheck autoCheck) {
        try {
            //将审核结束时间加23小时59分59秒
            LocalDateTime endTime = autoCheck.getEndTime().plusSeconds(86399);
            autoCheck.setEndTime(endTime);
            if (null != autoCheck) {
                iOmsAutoCheckService.save(autoCheck);
            }
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "更新 自动审单 列表", notes = "更新 自动审单 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping
    public Result update(@RequestBody OmsAutoCheck autoCheck) {
        try {
            if (null != autoCheck) {
                boolean flag = iOmsAutoCheckService.updateByCode(autoCheck);
                if (flag) {
                    return Result.ok("操作成功");
                } else {
                    return Result.error("操作失败");
                }
            }
            return Result.error("数据不可为空");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 启用自动审单
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "启用 自动审单", notes = "启用 自动审单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("active")
    public Result active(@RequestParam String code, HttpServerRequest request) {
        if (StringUtils.isBlank(code)) {
            return Result.error("未获取到需要启用的记录");
        }
        boolean flag = iOmsAutoCheckService.batchUpdateStatus(code, StatusEnum.ACTIVE.getValue());
        return flag ? Result.ok("启用成功") : Result.error("启用失败");
    }

    /**
     * 停用自动审单
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "停用 自动审单", notes = "停用 自动审单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("unactive")
    public Result unactive(@RequestParam String code, HttpServerRequest request) {
        if (StringUtils.isBlank(code)) {
            return Result.error("未获取到需要停用的记录");
        }
        boolean flag = iOmsAutoCheckService.batchUpdateStatus(code, StatusEnum.UNACTIVE.getValue());
        return flag ? Result.ok("停用成功") : Result.error("停用失败");
    }

    /**
     * 删除 自动审单
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "停用 自动审单", notes = "停用 自动审单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @DeleteMapping
    public Result delete(@RequestParam List<String> codes, HttpServerRequest request) {
        if (CollectionUtil.isEmpty(codes)) {
            return Result.error("未获取到需要删除的记录");
        }
        Map<String, Integer> result = iOmsAutoCheckService.deleteByCodes(codes);
        Integer success = MapUtil.getInt(result, PropertyConst.SUCCESS);
        int failNum = codes.size() - success;
        return Result.ok("删除成功" + success + "条；失败" + failNum + "条；");
    }


    @ApiOperation(value = "获取 自动审单 详情", notes = "获取 自动审单 详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsAutoCheck.class))
    @NeedLogin
    @GetMapping("/detail/{code}")
    public Result getInfoByCode(@PathVariable(value = "code", required = true) String code) {
        try {
            OmsAutoCheck autoCheck = iOmsAutoCheckService.getDetailInfoByCode(code);
            return Result.ok().data(autoCheck);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }
}
